// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'
import Client from '@api/parking/api/ParkSpacesApi'

import ComClass from '@api/ComQuery'
import ListClass from '@api/parking/model/ParkSpaceListQueryReq'
import OneClass from '@api/parking/model/ParkSpaceDetailQueryReq'
import AddClass from '@api/parking/model/ParkSpaceInsertReq'
import AddMultiClass from '@api/parking/model/ParkSpaceBatchInsertReq'
import EditClass from '@api/parking/model/ParkSpaceUpdateReq'
import DelClass from '@api/parking/model/ParkSpaceDeleteReq'
import BaseClass from '@api/parking/model/ParkSpaceDto'

const CLIENT = CORE.grpcCLIENT(Client)
const COM = [ComClass, 'comQuery']
const BASE = [BaseClass, 'parkSpaceDto']

// ============================== 导出远程接口 ============================== //

export default {
  // —————————————————————————————— 列表 —————————————————————————————— //

  async getList(param, TIP = '查询车位列表') {
    const DTO = CORE.grpcDTO(ListClass, param, COM)
    const resp = await CORE.grpc(TIP, CLIENT, 'queryParkSpaceList', DTO)
    return [resp.comQuery.totalCount, resp.parkSpaceDto || []]
  },

  // —————————————————————————————— 单项 —————————————————————————————— //

  async getOne({ id }, TIP = '查询车位详情') {
    const DTO = CORE.grpcDTO(OneClass, { id })
    const resp = await CORE.grpc(TIP, CLIENT, 'queryParkSpaceDetail', DTO)
    return resp.parkSpaceDto || {}
  },

  // —————————————————————————————— 增 —————————————————————————————— //

  async add(one, TIP = '新增车位') {
    const DTO = CORE.grpcDTO(AddClass, one, BASE)
    await CORE.grpc(TIP, CLIENT, 'insertParkSpace', DTO)
    return CORE.grpcOK(TIP)
  },

  // ——————————————————————————————批量新增 —————————————————————————————— //

  async addMulti(one, TIP = '批量新增车位') {
    const DTO = CORE.grpcDTO(AddMultiClass, one)
    await CORE.grpc(TIP, CLIENT, 'batchInsertParkSpace', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 改 —————————————————————————————— //

  async edit(one, TIP = '修改车位') {
    const DTO = CORE.grpcDTO(EditClass, one, BASE)
    await CORE.grpc(TIP, CLIENT, 'updateParkSpace', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 删 —————————————————————————————— //

  async del({ id }, TIP = '删除车位') {
    const DTO = CORE.grpcDTO(DelClass, { ids: [id] })
    await CORE.grpc(TIP, CLIENT, 'deleteParkSpace', DTO)
    return CORE.grpcOK(TIP)
  },
}
